package com.kaizzi.data.jdl.helper;

import com.kaizzi.data.common.been.TempBehavior;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.common.utils.StringUtil;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.service.MotionClocksService;
import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/16
 */
@Component
public class PageData42 {

    @Resource
    private MotionClocksService motionClocksService;

    public String getData(ExcelModel model){

        Page42 page42 = new Page42();

        String ytdDate = DateUtil.getYtdDate();
        List<String> transTime = DateUtil.getTransTime(ytdDate);

        List<TempBehavior> yfFission = motionClocksService.getYfFission2(transTime.get(0), transTime.get(1));
        List<TempBehavior> aPublic = motionClocksService.getPublic2(transTime.get(0), transTime.get(1));
        List<TempBehavior> yfKoc = motionClocksService.getYfKoc2(transTime.get(0), transTime.get(1));
        List<TempBehavior> marathon = motionClocksService.getMarathon2(transTime.get(0), transTime.get(1));
        List<TempBehavior> wes = motionClocksService.getWes2(transTime.get(0), transTime.get(1));

        if (yfFission== null ||yfFission.size() == 0){
            page42.setYfFission(getTempPage42(1));
        } else {
            TempPage42 dataTempPage42 = getDataTempPage42(yfFission, 1);
            page42.setYfFission(dataTempPage42);
        }

        if (aPublic == null || aPublic.size() == 0){
            page42.setAPublic(getTempPage42(2));
        }else {
            TempPage42 dataTempPage42 = getDataTempPage42(aPublic, 2);
            page42.setAPublic(dataTempPage42);
        }

        if (yfKoc == null || yfKoc.size() == 0){
            page42.setYfKoc(getTempPage42(3));
        }else {
            TempPage42 dataTempPage42 = getDataTempPage42(yfKoc, 3);
            page42.setYfKoc(dataTempPage42);
        }

        if (marathon == null || marathon.size() == 0){
            page42.setMarathon(getTempPage42(4));
        }else {
            TempPage42 dataTempPage42 = getDataTempPage42(marathon, 4);
            page42.setMarathon(dataTempPage42);
        }

        if (wes == null || wes.size() == 0){
            page42.setWes(getTempPage42(5));
        }else {
            TempPage42 dataTempPage42 = getDataTempPage42(wes, 5);
            page42.setWes(dataTempPage42);
        }

        page42.setDataPeriod(DateUtil.getFirstDay2LastSunday());
        page42.setTitle(LogicUtil.getTitle(model,42));

        return JsonUtil.serialize(page42);
    }

    private String getChildTitle(String childTitle){
        return childTitle.replace("（","（'").replace("）","'）");
    }

    private TempPage42 getDataTempPage42(List<TempBehavior> list, int no){

        double count = list.stream().mapToDouble(TempBehavior::getTempCount).sum();
        double users = list.size();
        double singleCount = getSingleCount(no);
        //兑换率
        String rate = StringUtil.decimalFormat(users / singleCount * 100)+"%";
        //封装对象
        TempPage42 tempPage42 = new TempPage42();
        tempPage42.setInteractiveRate(rate);
        tempPage42.setExchangeUsers(StringUtil.pointFormat(users));
        tempPage42.setExchangeCount(StringUtil.pointFormat(count));
        List<String> textList = new ArrayList<>();
        list.forEach(been -> {
            textList.add(been.getTempTitle());
        });
        //去重，同一名称显示一次
        List<String> collect = textList.stream().distinct().collect(Collectors.toList());
//        System.out.println(collect);
        tempPage42.setTexts(collect);
        tempPage42.setChildTitle(getChildTitle(getSingleName(no)));

        return tempPage42;
    }

    private TempPage42 getTempPage42(int no){
        TempPage42 tempPage42 = new TempPage42();
        tempPage42.setExchangeCount("0");
        tempPage42.setExchangeUsers("0");
        tempPage42.setInteractiveRate("0");
        tempPage42.setTexts(new ArrayList<>());
        tempPage42.setChildTitle(getChildTitle(getSingleName(no)));
        return tempPage42;
    }

    public double getSingleCount(int no){
        double result = 0;
        switch (no){
            case 1:
                result = 32323;
                break;
            case 2:
                result = 5125;
                break;
            case 3:
                result = 149;
                break;
            case 4:
                result = 2992;
                break;
            case 5:
                result = 6988;
                break;
            default:
                break;
        }
        return result;
    }

    //名称
    public String getSingleName(int no){
        String result = "";
        switch (no){
            case 1:
                result = "有风社交裂变用户（32,323）";
                break;
            case 2:
                result = "公众号投放（5,125）";
                break;
            case 3:
                result = "有风KOC（149）";
                break;
            case 4:
                result = "马拉松用户（2,992）";
                break;
            case 5:
                result = "威尔仕用户（6,988）";
                break;
            default:
                break;
        }
        return result;
    }

    @Data
    private static class TempPage42{
        private String childTitle;
        private String exchangeCount;
        private String exchangeUsers;
        private String interactiveRate;
        private List<String> texts;
    }

    @Data
    private static class Page42{
        private String title;
        private String dataPeriod;
        private TempPage42 yfFission;
        private TempPage42 aPublic;
        private TempPage42 yfKoc;
        private TempPage42 marathon;
        private TempPage42 wes;
    }
}
